package logic

import (
	"context"
	"edu/edurpc/edurpc"
	"edu/edurpc/internal/svc"
	"edu/models/mysql"
	"fmt"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
)

type ContentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewContentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ContentAddLogic {
	return &ContentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ContentAddLogic) ContentAdd(in *edurpc.ContentAddRequest) (*edurpc.ContentAddResponse, error) {
	var novel mysql.Novel
	mysql.DB.Where("id = ?", in.NovelId).First(&novel)
	if novel.ID == 0 {
		return nil, fmt.Errorf("小说不存在")
	}
	if strings.Contains(in.Context, "抄袭") || strings.Contains(in.Context, "涉政") {
		return nil, fmt.Errorf("检测评论内容敏感词抄袭涉政,拦截违规内容")
	}
	content := mysql.Content{
		UserId:  int(in.UserId),
		NovelId: int(in.NovelId),
		Context: in.Context,
	}
	mysql.DB.Create(&content)
	return &edurpc.ContentAddResponse{
		ContentId: int64(content.ID),
	}, nil
}
